
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileInputStream;
import java.util.ArrayList;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.Timer;
import sun.audio.AudioData;
import sun.audio.AudioDataStream;
import sun.audio.AudioPlayer;
import sun.audio.AudioStream;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author Henri
 */
public class Ponorka extends JLabel implements ActionListener {

    private Panel tusom;
    private Timer t;
    protected int smer;
    boolean vybuchnuta = false;
    static ArrayList strely = new ArrayList();
//    ImageIcon p1 = new ImageIcon("img/P1.png");
//    ImageIcon p2 = new ImageIcon("img/P2.png");
//    ImageIcon l1 = new ImageIcon("img/L1.png");
//    ImageIcon l2 = new ImageIcon("img/L2.png");
//    ImageIcon pon_exploP = new ImageIcon("img/pon_exploP.png");
//    ImageIcon pon_exploL = new ImageIcon("img/pon_exploL.png");
    ImageIcon p1 = new javax.swing.ImageIcon(getClass().getResource("img/P1.png"));
    ImageIcon p2 = new javax.swing.ImageIcon(getClass().getResource("img/P2.png"));
    ImageIcon l1 = new javax.swing.ImageIcon(getClass().getResource("img/L1.png"));
    ImageIcon l2 = new javax.swing.ImageIcon(getClass().getResource("img/L2.png"));
    ImageIcon pon_exploP = new javax.swing.ImageIcon(getClass().getResource("img/pon_exploP.png"));
    ImageIcon pon_exploL = new javax.swing.ImageIcon(getClass().getResource("img/pon_exploL.png"));
    AudioPlayer ap = AudioPlayer.player;
    AudioStream as;
    AudioData ad;
    AudioDataStream zvuk = null;
    Rectangle rectangle;

    public Ponorka(int x, int y, Panel tusom, int cas) {
        setBounds(x, y, 51, 14);
        t = new Timer(100 / cas + 10, this);
        this.tusom = tusom;
        t.start();
        this.setIcon(p1);
        smer = 1;
        this.rectangle = new Rectangle(x, y, 51, 14);
    }

    public static ArrayList getstrely() {
        return strely;
    }

    public void vybuchni() {
        zasiahnuta();
        if (smer == 1) {
            this.setIcon(pon_exploP);
        } else {
            this.setIcon(pon_exploL);
        }
        vybuchnuta = true;
    }

    public void vystrelzvuk() {

        try {
            as = new AudioStream(new FileInputStream("./zvuky/vystrel_ponorka.wav"));
            ad = as.getData();
            zvuk = new AudioDataStream(ad);
        } catch (Exception e) {
            System.out.println(e);
        }
        ap.start(zvuk);
    }

    public void zasiahnuta() {

        try {
            as = new AudioStream(new FileInputStream("./zvuky/vybuch_ponorky.wav"));
            ad = as.getData();
            zvuk = new AudioDataStream(ad);
        } catch (Exception e) {
            System.out.println(e);
        }
        ap.start(zvuk);
    }

    public void vystrel() {
        int x = getX() + 15;
        int y = getY() - 5;
        strelaPonorka s = new strelaPonorka(x, y, tusom);
        tusom.add(s);
        strely.add(s);
        vystrelzvuk();
    }

    protected void pohybD() {
        int zmena = getY() - 5;
        setLocation(getX(), zmena);
    }

    protected void pohybP() {
        int zmena = getX() + 5;
        setLocation(zmena, getY());

    }

    protected void pohybL() {
        int zmena = getX() - 5;
        setLocation(zmena, getY());
    }
    int im = 0;

    protected void zmenaob() {
        if (smer == 1) {
            if (im == 0) {
                this.setIcon(p2);
                im = 1;
            } else {
                this.setIcon(p1);
                im = 0;
            }
        } else if (smer == -1) {
            if (im == 0) {
                this.setIcon(l2);
                im = 1;
            } else {
                this.setIcon(l1);
                im = 0;

            }
        }
    }
    public void zmizni(){
        remove(this);
        t.stop();
    }

    public void actionPerformed(ActionEvent e) {
        if (vybuchnuta == true) {
            tusom.remove(this);
            t.stop();
            rectangle.setLocation(800, 0);
            repaint();
            return;
        }
        rectangle.setLocation(getX(), getY());
        zmenaob();

        int x = (int) (1 + Math.random() * 60 );
        if (x == 17) {
            vystrel();
        }
        if (smer == 1) {
            pohybP();
            if (getX() > 343) {
                smer = -1;

            }
        } else {
            pohybL();
            if (getX() < 1) {
                smer = 1;

            }
        }
        rectangle.setLocation(getX(), getY());

    }
}
